﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml;

public partial class PackingTask : System.Web.UI.Page
{
    readonly static string DATAMART_DB = ConfigurationManager.ConnectionStrings["DataMartDB"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ClearContent();
        string StoreId = Request["StoreID"];
        if (string.IsNullOrEmpty(StoreId))
        {
            Response.AddHeader("Result", "StoreID is NulOrEmpty");
        }
        else
        {
            string BusinessDate = string.Empty;
            try
            {
                using (SqlConnection con = new SqlConnection(DATAMART_DB))
                {
                    string cmdText = "SELECT ISNULL(MAX(Business_Date), '2006-3-29 0:0:0') AS BusinessDate FROM Log_PosData WHERE (Store_ID = @storeid) AND (Error_Type = 'OK')";
                    using (SqlCommand com = new SqlCommand(cmdText, con))
                    {
                        com.Parameters.AddWithValue("@storeid", StoreId);
                        con.Open();
                        using (SqlDataReader dr = com.ExecuteReader())
                        {
                            if (dr.Read())
                            {
                                BusinessDate = dr["BusinessDate"].ToString();
                            }
                            dr.Close();
                        }
                    }
                }
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                xmlWriterSettings.Indent = true;
                using (XmlWriter writer = XmlWriter.Create(Response.OutputStream, xmlWriterSettings))
                {
                    writer.WriteStartElement("TaskList");

                    writer.WriteStartElement("PosDataPacking");
                    writer.WriteStartElement("Task");
                    writer.WriteAttributeString("id", StoreId + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"));
                    writer.WriteAttributeString("type", "DateRange");
                    writer.WriteAttributeString("date1", BusinessDate);
                    writer.WriteAttributeString("date2", DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
                    writer.WriteAttributeString("value", "");
                    writer.WriteEndElement();//Task
                    writer.WriteEndElement();//PosDataPacking                    
                    writer.WriteEndElement();//TaskList
                    writer.Flush();
                    writer.Close();
                }
                xmlWriterSettings = null;
            }
            catch (Exception EX)
            {


                Response.AddHeader("Result", EX.ToString());
            }

        }//end if   
        Response.End();
    }
}
